package com.micai.springboot.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.micai.springboot.entity.MenuEntity;
import com.micai.springboot.service.MenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 描述：菜单管理
 * <p>
 * Author: 赵新国
 * Date: 2017/11/14 11:35
 */
@Controller
@RequestMapping("/admin/menu")
public class AdminMenuController {

    private static final Logger logger = LoggerFactory.getLogger(AdminMenuController.class);

    @Autowired
    private MenuService menuService;

    /**
     * 分页查询菜单列表
     * @param model
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/list")
    public String list(@RequestParam(value="pageNum", defaultValue="1") int pageNum,
                       @RequestParam(value="pageSize", defaultValue="5") int pageSize,
                       @RequestParam(required = false) String name, Model model) {
        Page<MenuEntity> menuEntities = menuService.getMenuByPage(pageNum, pageSize, name);
        if (menuEntities == null && menuEntities.getResult().isEmpty()) {
            return null;
        }
        PageInfo<MenuEntity> page = new PageInfo<MenuEntity>(menuEntities);
        model.addAttribute("page",page);
        // 参数回显
        model.addAttribute("name", name);
        return "admin/menu/menuList";
    }

    @RequestMapping(value = "/tree")
    public String tree(@RequestParam(required = false) String name, Model model) {
        List<MenuEntity> menuEntities = menuService.getMenuByTree(name);
        if (menuEntities == null && menuEntities.isEmpty()) {
            return null;
        }
        logger.info("menuEntities: {}", JSONObject.toJSONString(menuEntities));
        return "admin/menu/menuList";
    }
}
